<?php

namespace model;

use common\Model;

class siteconfig extends Model
{

	const SITEINFO = "siteinfo";
	const CACHE = "cache";
	const UPLOAD = "upload";
	const THEME = "theme";
	const TABLE = "siteconfig";

	protected $DB;

	function __construct()
	{
		$this->DB = parent::db();
	}

	public function FindData(string $val = null)
	{
		$result = $this->DB->table(self::TABLE)->cache(0)->select();
		$siteinfo = array_column(FilterArrayByValue($result, 'name', self::SITEINFO), 'data');
		$cache = array_column(FilterArrayByValue($result, 'name', self::CACHE), 'data');
		$upload = array_column(FilterArrayByValue($result, 'name', self::UPLOAD), 'data');
		$theme = array_column(FilterArrayByValue($result, 'name', self::THEME), 'data');
		$data = [
			self::SITEINFO => JsonDecode($siteinfo[0]),
			self::CACHE    => JsonDecode($cache[0]),
			self::UPLOAD   => JsonDecode($upload[0]),
			self::THEME    => JsonDecode($theme[0])
		];
		if ($val != null) return $data[$val];
		else return $data;
	}


	//更新数据
	public function updateData()
	{
		$data = [];
		$data[self::SITEINFO] = [
			'sitename' => VarFilter($_POST['sitename']),
			'siteurl' => VarFilter($_POST['siteurl']),
			'keywords' => VarFilter($_POST['keywords']),
			'descript' => VarFilter($_POST['descript']),
			'copyright' => VarFilter($_POST['copyright']),
		];

		$data[self::CACHE] = [
			'indexhtml' => VarFilter($_POST['indexhtml']),
			'indexhtmltime' => VarFilter($_POST['indexhtmltime']),
			'visitscache' => VarFilter($_POST['visitscache']),
			'visitsnum' => VarFilter($_POST['visitsnum']),
			'visitstime' => VarFilter($_POST['visitstime']),
			'visitsshowtime' => VarFilter($_POST['visitsshowtime']),
			'publishtime' => VarFilter($_POST['publishtime']),
			'publishyear' => VarFilter($_POST['publishyear']),
			'publishcachetime' => VarFilter($_POST['publishcachetime']),//后台：发布统计缓存时间
		];

		$data[self::UPLOAD] = [
			'imguploadsize' => VarFilter($_POST['imguploadsize']),
			'attuploadsize' => VarFilter($_POST['attuploadsize']),
			'imguploadext' => VarFilter($_POST['imguploadext']),
			'attuploadext' => VarFilter($_POST['attuploadext']),
			'thumbON' => VarFilter($_POST['thumbON']),
			'maxWidth' => VarFilter($_POST['maxWidth']),
			'maxHeight' => VarFilter($_POST['maxHeight']),
			'waterON' => VarFilter($_POST['waterON']),
			'waterImg' => VarFilter($_POST['waterImg']),
			'waterPos' => VarFilter($_POST['waterPos']),
			'videouploadext' => VarFilter($_POST['videouploadext']),
			'videouploadsize' => VarFilter($_POST['videouploadsize']),
		];

		$data[self::THEME] = [
			'indextheme' => VarFilter($_POST['indextheme']),
		];


		$siteinfo = JsonEncode($data[self::SITEINFO]);
		$cache = JsonEncode($data[self::CACHE]);
		$upload = JsonEncode($data[self::UPLOAD]);
		$theme = JsonEncode($data[self::THEME]);


		//批量更新系统配置
		$pdo = \libs\pdo::Init();
		$prefix = $pdo->GetConfig();
		$fix = $prefix['prefix'] . self::TABLE;
		$sql = "UPDATE {$fix} SET data = (CASE ";
		$sql .= "WHEN name = 'siteinfo' THEN '{$siteinfo}' ";
		$sql .= "WHEN name = 'cache' THEN '{$cache}' ";
		$sql .= "WHEN name = 'upload' THEN '{$upload}' ";
		$sql .= "WHEN name = 'theme' THEN '{$theme}' ";
		$sql .= "END) WHERE name IN ('siteinfo','upload','cache','theme')";
		$re = $pdo->submit($sql);

		if ($re > 0) {
			SqlLog("修改系统配置 {$re} 条");
			CleanDbCache(self::TABLE);
			return ['status' => 1, 'msg' => '修改成功'];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化！'];
		}
	}

	public function getUpload()
	{
		return $this->FindData(self::UPLOAD);
	}

	public function getCache()
	{
		return $this->FindData(self::CACHE);
	}
	
	//根据数组键名查找对应的系统设置信息
	public function getSiteInfo($key)
	{
		$result = $this->FindData(self::SITEINFO);
		return $result[$key];
	}

}
